<?php

namespace component\element\select\controller;

use app\attribute\MethodAuth;
use app\bll\Datadict as BllDatadict;
use core\component\ElementBase;
use support\Db;
use support\View;

class Index extends ElementBase
{
    public function index(...$param)
    {
        switch ($param['sourcetype']) {
            case 1:
                $blDatadict = new BllDatadict();
                $dict = $blDatadict->find($param['sourceval']);
                View::assign('datasource', $dict['values']);
                View::assign('displayvalue', 'displayname');
                View::assign('realvalue', 'realvalue');
                break;
            case 2:
                $sourceval = explode("\n", str_replace("\r\n", "\n", $param['sourceval']));

                foreach ($sourceval as $key => $val) {
                    list($cusrealvalue, $cusdisplayname) = explode('|', $val);
                    $datasource[] = [
                        'displayname' => $cusdisplayname,
                        'realvalue'   => $cusrealvalue
                    ];
                }
                View::assign('datasource', $datasource);
                View::assign('displayvalue', 'displayname');
                View::assign('realvalue', 'realvalue');

                break;
        }
        return $this->view($param);
    }
}
